﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Resources;
using IdeaBlade.EntityModel;

namespace CoProjectModel
{
    public interface ISampleDataLoader
    {
        EntityCacheState SampleData { get; }
    }

    [Export(typeof(ISampleDataLoader))]
    public class SampleDataLoader : ISampleDataLoader
    {
        private EntityCacheState _sampleData;

        #region ISampleDataLoader Members

        public EntityCacheState SampleData
        {
            get { return _sampleData ?? (_sampleData = GetSampleData()); }
        }

        #endregion

        private EntityCacheState GetSampleData()
        {
            const string filename = @"/CoProjectModel.SL;component/SampleData.xml";

            StreamResourceInfo res = Application.GetResourceStream(new Uri(filename, UriKind.Relative));

            if (null == res)
                throw new InvalidOperationException("Couldn't find design ECS at " + filename);

            return EntityCacheState.Restore(res.Stream, useBinaryFormat: false);
        }
    }
}